import authService from './utils/auth';
import https from './utils/http';
import toastService from './utils/toast.service';
// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })

    wx.onBeforeAppRoute(function (res) {
      // 路由发生变化，判断是否登入
      if (res.path !== 'pages/login/login') {
        authService.checkIsLogin().then(result => { 
          console.log("result:",result)
          if(!result){
            wx.navigateTo({ 
              url: '/pages/login/login',
              fail(e) {
                console.log(e)
              }
            });
          }
        })
        
      }
    })
    
    this.getSystemInfo(); 
  },
  getUserInfo() { 
    https.request('/api-merchant/rest/merchant/getLoginMerchantInfo', {}).then(result => { 
      if (result.success) { 
        //console.log(result.data); 
        this.globalData.loginUserInfo = result.data; 
        //console.log(this.globalData.userInfo); 
      } 
    }) 
  },
  getSystemInfo() { 
    var res = wx.getSystemInfoSync(); 
    this.globalData.systemInfoSync = res; 
  },
  globalData: {
    userInfo: null
  }
})
